<?php

namespace App\Http\Controllers\Home\Sign;

use App\Api\Helpers\ApiResponse;
use App\Http\Controllers\Controller;
use App\Models\User;
use App\Models\UserNew;
use Illuminate\Http\Request;
use App\Models\Sign\Project;
use Illuminate\Support\Facades\DB;

class ProjectController extends Controller
{
    use ApiResponse;

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     *
     *显示资源列表 ：  显示文章列表
     */
    public function index()
    {
        //
        $project_data = Project::select('id','pid','name','status')->get();

        $project_count = Project::count();//总数


        $message = [];
        foreach ($project_data as $key=>$val){
            $message[$val->id]['id'] = $val->id;
            $message[$val->id]['pid'] = $val->pid;
            $message[$val->id]['name'] = $val->name;
            $message[$val->id]['status'] = $val->status;
        }


        //树形-联动
        $project_msg = [];
        foreach ($message as $key=>$value){

            if($value['pid'] == 0 ){
                $project_msg[] = &$message[$value['id']];
            }else{
                $message[$value['pid']]['son'][] = &$message[$value['id']];
            }
        }

//return $project_msg;
       return view('home.project.index')
           ->with('count',$project_count)
           ->with('msg',$project_msg);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     *
     * 显示用于创建新资源的窗体 ：创建表单页面
     */
    public function create()
    {
        //

        $project_data = Project::select('id','pid','name')->get();

        $message = [];
        foreach ($project_data as $key=>$val){
            $message[$val->id]['id'] = $val->id;
            $message[$val->id]['pid'] = $val->pid;
            $message[$val->id]['name'] = $val->name;
        }

        //树形-联动
        $project_msg = [];
        foreach ($message as $key=>$value){

            if($value['pid'] == 0 ){
                $project_msg[] = &$message[$value['id']];
            }else{
                $message[$value['pid']]['son'][] = &$message[$value['id']];
            }
        }

        return view('home.project.create')->with('msg',$project_msg);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     *
     * 将新创建的资源存储在存储器中 :创建
     */
    public function store(Request $request)
    {
        $project_name = $request->name;//添加顶级栏目

        $add = Project::insert([
            'pid' => 0,
            'name' => $project_name,
        ]);

        return $project_name;
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     *
     * 显示指定的资源
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     *
     * 显示用于编辑指定资源的窗体  修改
     */
    public function edit($id)
    {

        $project_id = $id;

        $data = Project::where('id',$project_id)->select('id','pid','name')->first();

        return view('home.project.edit  ')->with('data',$data);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     *
     * 更新存储中的指定资源  提交：更新
     */
    public function update(Request $request, $id)
    {

        $project_id = $request->id;
        $project_name = $request->name; //报考专业信息

        $update = Project::where('id',$project_id)->update([
            'name'=>$project_name
        ]);

        return $update;
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
        return 1111;
    }


    //创建子栏目页面数据
    public function createSonProject(Request $request)
    {
        $id = $request->id;

        $data = Project::where('id',$id)->select('id','pid','name' )->first();

        return view('home.project.sonCreate')->with('data',$data);

    }

    //添加子栏目
    public function addSonProject(Request $request)
    {

        $id = $request->id;//父级栏目id
        $name = $request->name;//次级栏目

        $ins =Project::create([
            'pid' => $id,
            'name' => $name

        ]);

        return $ins;
    }


    //删除报考专业栏目
    public function deleteProject(Request $request)
    {

        $id = $request->id;


        //是否存在子集
        $project = Project::where('pid',$id)->count();

       if (empty($project)){
           //无子集，删除不影响下级栏目，所以可以执行此模块
           $delete = Project::where('id',$id)->delete();

           return $this->success($id);

       }else{

           /**
            * 因为有子集栏目，不宜删除，暂不使用此模块
            */
           //有子集，将父子集一并删除
//           $project_data = Project::where('pid',$id)->get();
//
//           foreach ($project_data as $key=>$val){
//
//                    $get_id[$key] = $val->id;
//           }
//           $id_data = array_values($get_id); //获取子集 id 的集合
//           $delete = Project::where('id',$id_data)->delete();
//           return $this->success($id_data,'delete many');


           //替换上文模块，暂时使用

           return $this->failed('有下级栏目，请先处理','203');

       }
    }


    //project开关
    public function switchProject(Request $request)
    {
        $id = $request->id;

        $status_data = Project::where('id',$id)->select('id','status')->first();

        if ($status_data->status == 1){
            //当status为1时修改为0，关闭

            $switch = Project::where('id',$status_data->id)->update(['status'=>0]);

            return  $this->success('关闭该选项');
        }else{
            //开启
            $switch = Project::where('id',$status_data->id)->update(['status'=>1]);

            return  $this->success('开启该选项');
        }

    }


    //百度 ueditor 编辑器
    public function ueditor()
    {
       return view('home.editor.ueditor');
    }

    public function addEditor(Request $request)
    {

//        $data['id'] = $request->id;
        $data['title'] = $request->name;
        $data['msg'] = $request->msg;//编辑器


//        if (empty($data['id'])){
//            return $this->failed('没有此用户','203');
//        }
//        $user_end =  User::where('id',$data['id'])->first();
//
//        if (empty($user_end)){
//            return $this->failed('没有此用户信息','203');
//        }
//
//        $install = UserNew::create($data);
//
//        if (!$install){
//
//            return $this->failed('添加失败','203');
//        }
        return $this->success('添加成功');
    }

}
